package com.bai.edu.controller;


import com.bai.edu.client.UcenterClient;
import com.bai.edu.entity.Comment;
import com.bai.edu.service.CommentService;
import com.bai.utils.JwtUtils;
import com.bai.utils.R;
import com.bai.utils.orderVo.UcenterMemberOrder;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 评论 前端控制器
 * </p>
 *
 * @author bai
 * @since 2023-08-19
 */
@RestController
@RequestMapping("/eduservice/comment")
public class CommentController {

    @Autowired
    private CommentService commentService;
    @Autowired
    private UcenterClient ucenterClient;

    //TODO:添加评论
    @ApiOperation("添加评论")
    @PostMapping("save")
    public R save(@RequestBody Comment comment, HttpServletRequest request) {
        String id = JwtUtils.getMemberIdByJwtToken(request);
        UcenterMemberOrder memberOrder = ucenterClient.getById(id);
        BeanUtils.copyProperties(memberOrder,comment);
        commentService.save(comment);
        return R.ok();
    }

    //TODO:根据课程idid分页弄查询评论
    @ApiOperation("分页查询评论")
    @GetMapping("getById/{page}/{limit}/{id}")
    public R getAllById(@PathVariable String id,
                        @PathVariable long page,
                        @PathVariable long limit) {
        Page<Comment> commentPage = new Page<>(page, limit);
        QueryWrapper<Comment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("course_id",id);
        IPage<Comment> commentIPage = commentService.page(commentPage, queryWrapper);
        List<Comment> commentList = commentIPage.getRecords();
        long size = commentIPage.getSize();
        long total = commentIPage.getTotal();
        long pages = commentIPage.getPages();
        long current = commentIPage.getCurrent();
        return R.ok().data("commentList",commentList)
                .data("size",size)
                .data("total",total)
                .data("pages",pages)
                .data("current",current);
    }


}

